/* sm_sea.c -- Mar
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 *                     Márcio Rogério
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "sm_sea.h"
#include "sm_utils.h"

sm_sea_t *
sm_sea_new( const int width, const int height )
{
    sm_sea_t * self = NULL;
    int i = 0;
    int j = 0;

    self = XMALLOC( sm_sea_t, 1 );

    self->water_grid = XMALLOC( GLint *, width );
    for( i = 0 ; i < width ; i++ )
	self->water_grid[i] = XMALLOC( GLint, height );

    /* Zerando Matriz */
    for( i = 0 ; i < width ; i++ )
	for( j = 0 ; j < height ; j++ )
	    self->water_grid[i][j] = 0;

    self->draw = (void*) &sm_sea_draw;

    self->width = width;
    self->height = height;

    /* Setting material */
    self->mat_specular[POS_RED]   = 1.0;
    self->mat_specular[POS_GREEN] = 1.0;
    self->mat_specular[POS_BLUE]  = 1.0;
    self->mat_specular[POS_ALPHA] = 1.0;
    self->mat_diffuse[POS_RED]    = 0.0;
    self->mat_diffuse[POS_GREEN]  = 1.0;
    self->mat_diffuse[POS_BLUE]   = 0.0;
    self->mat_diffuse[POS_ALPHA]  = 1.0;
    self->mat_ambient[POS_RED]    = 1.0;
    self->mat_ambient[POS_GREEN]  = 1.0;
    self->mat_ambient[POS_BLUE]   = 1.0;
    self->mat_ambient[POS_ALPHA]  = 1.0;
    self->mat_shininess           = 100.0;

    return self;
}

void
sm_sea_destroy( sm_sea_t * self )
{
    int i = 0;

    for( i = 0 ; i < self->width ; i++ )
	XFREE(self->water_grid[i]);
    XFREE( self->water_grid );
}

void
sm_sea_set_material( sm_sea_t * self )
{
    /* Setando as características dos materiais */

    glMaterialfv(GL_FRONT, GL_SPECULAR, self->mat_specular);
    glMaterialfv(GL_FRONT, GL_AMBIENT, self->mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, self->mat_diffuse);
    glMaterialf(GL_FRONT, GL_SHININESS, self->mat_shininess);
}
void
sm_sea_draw( sm_sea_t *self )
{

    GLfloat array[] =
	{
	    
	};
    /* Desenha o plano */
    glPushMatrix();
    {
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);

	sm_sea_set_material( self );
	glBegin(GL_QUADS);{	
	    glVertex3i(-(self->width)>>1, 0.0,  (self->height)>>1 );
	    glVertex3i( (self->width)>>1, 0.0,  (self->height)>>1 );
	    glVertex3i( (self->width)>>1, 0.0, -(self->height)>>1 );
	    glVertex3i(-(self->width)>>1, 0.0, -(self->height)>>1 );
	}glEnd();
    }glPopMatrix();

}
